package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class PersonalSettingsPage extends Utils {
    public static String url = "http://49.235.61.184:8080/system/setting";

    public PersonalSettingsPage() {
        super(url);
    }
    /**
     * 检查页面元素
     */
    public void checkPersonalSettingsPageRight() throws IOException {
        //检查“基本设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li.ant-menu-item.ant-menu-item-selected.ant-menu-item-only-child > span"));
        //检查“安全设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li:nth-child(2) > span"));
        //检查“AI设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li:nth-child(3) > span"));
        //检查“系统设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li:nth-child(4) > span"));
        //检查姓名输入框
        driver.findElement(By.cssSelector("#name"));
        //检查手机号输入框
        driver.findElement(By.cssSelector("#phone"));
        //检查邮箱输入框
        driver.findElement(By.cssSelector("#email"));
        //检查个人简介输入框
        driver.findElement(By.cssSelector("#profile"));
        //检查“提交”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button"));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 测试一：提交基本信息
     */
    public void submitBasicInformationSuc() throws IOException {
        //刷新
        driver.navigate().refresh();
        //输入姓名
        driver.findElement(By.cssSelector("#name")).sendKeys("霖霖");
        //输入手机号
        driver.findElement(By.cssSelector("#phone")).sendKeys("13727357843");
        //输入邮箱
        driver.findElement(By.cssSelector("#email")).sendKeys("3847563783@qq.com");
        //输入个人简介
        driver.findElement(By.cssSelector("#profile")).sendKeys("linlin");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“提交”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //验证是否修改成功
        String str = driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > header.ant-layout-header.ant-pro-layout-header.ant-pro-layout-header-fixed-header.ant-pro-layout-header-mix.ant-pro-layout-header-header > div > div.ant-pro-global-header-right-content > div > div > div > span > div > span:nth-child(2)")).getText();
        assert str.equals("霖霖");
    }
    /**
     * 测试二：修改失败-姓名为空
     */
    public void updateInformationFile1() throws IOException {
//刷新
        driver.navigate().refresh();
        //输入手机号
        driver.findElement(By.cssSelector("#phone")).sendKeys("13727357843");
        //输入邮箱
        driver.findElement(By.cssSelector("#email")).sendKeys("3847563783@qq.com");
        //输入个人简介
        driver.findElement(By.cssSelector("#profile")).sendKeys("linlin");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“提交”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //验证是否修改成功
        String str = driver.findElement(By.cssSelector("#name_help > div")).getText();
        assert str.equals("请输入您的昵称!");
    }
    /**
     * 测试三：修改失败-手机号为空
     */
    public void updateInformationFile2() throws IOException {
        //刷新
        driver.navigate().refresh();
        //输入姓名
        driver.findElement(By.cssSelector("#name")).sendKeys("霖霖");
        //输入邮箱
        driver.findElement(By.cssSelector("#email")).sendKeys("3847563783@qq.com");
        //输入个人简介
        driver.findElement(By.cssSelector("#profile")).sendKeys("linlin");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“提交”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //验证是否修改成功
        String str = driver.findElement(By.cssSelector("#phone_help > div")).getText();
        assert str.equals("请输入您的联系电话!");
    }
    /**
     * 测试四：修改失败-邮箱为空
     */
    public void updateInformationFile3() throws IOException {
        //刷新
        driver.navigate().refresh();
        //输入姓名
        driver.findElement(By.cssSelector("#name")).sendKeys("霖霖");
        //输入手机号
        driver.findElement(By.cssSelector("#phone")).sendKeys("13727357843");
        //输入个人简介
        driver.findElement(By.cssSelector("#profile")).sendKeys("linlin");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“提交”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //验证是否修改成功
        String str = driver.findElement(By.cssSelector("#email_help > div")).getText();
        assert str.equals("请输入您的邮箱!");
    }
    /**
     * 测试五：修改失败-个人简介为空
     */
    public void updateInformationFile4() throws IOException {
        //刷新
        driver.navigate().refresh();
        //输入姓名
        driver.findElement(By.cssSelector("#name")).sendKeys("霖霖");
        //输入手机号
        driver.findElement(By.cssSelector("#phone")).sendKeys("13727357843");
        //输入邮箱
        driver.findElement(By.cssSelector("#email")).sendKeys("3847563783@qq.com");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“提交”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //验证是否修改成功
        String str = driver.findElement(By.cssSelector("#profile_help > div")).getText();
        assert str.equals("请输入个人简介!");
    }

    /**
     * 测试六：修改账户密码
     */
    public void updatePassword() throws IOException, InterruptedException {
        //刷新
        driver.navigate().refresh();
        //点击“安全设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li:nth-child(2) > span")).click();
        //点击“修改”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.ant-list.ant-list-split > div > div > ul > li > ul > li > a")).click();
        //输入旧密码
        driver.findElement(By.cssSelector("#oldPassword")).sendKeys("123456");
        //输入新密码
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");
        //输入确认密码
        driver.findElement(By.cssSelector("#confirmPassword")).sendKeys("111111");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        /*//点击“确认修改”按钮
        driver.findElement(By.cssSelector("body > div:nth-child(8) > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > div > div > div:nth-child(2) > button")).click();
        //屏幕截图*/
        // 点击“确认修改”按钮
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        // 等待弹窗按钮容器可见，直接定位“主按钮”（Ant Design 弹窗的确认按钮默认是 primary 类型）
        WebElement confirmBtn = wait.until(ExpectedConditions.elementToBeClickable(
                By.cssSelector(".ant-modal-content .ant-modal-footer .ant-btn-primary")
        ));
        confirmBtn.click();
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > header.ant-layout-header.ant-pro-layout-header.ant-pro-layout-header-fixed-header.ant-pro-layout-header-mix.ant-pro-layout-header-header > div > div.ant-pro-global-header-right-content > div > div > div > div:nth-child(2) > span > svg")).click();

        //用 XPath 文本定位
        WebElement surveyItem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@class, 'ant-dropdown-menu')]//span[text()='退出登录']")));
        // 滚动到元素 + JS强制点击
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center'});", surveyItem);
        Thread.sleep(1000);
        ((JavascriptExecutor) driver).executeScript("arguments[0].click();", surveyItem);
        Thread.sleep(1000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //输入正确的账号和密码
        driver.findElement(By.cssSelector("#username")).sendKeys("lin");
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();
        //检测是否登陆成功
        String str = driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.title___3cGwa")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert str.equals("基本设置");

    }
    /**
     * 测试七：修改账户密码失败-旧密码为空
     */
    public void updatePasswordFile1() throws IOException, InterruptedException {
        //刷新
        driver.navigate().refresh();
        //点击“安全设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li:nth-child(2) > span")).click();
        //点击“修改”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.ant-list.ant-list-split > div > div > ul > li > ul > li")).click();

        //输入新密码
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        //输入确认密码
        driver.findElement(By.cssSelector("#confirmPassword")).sendKeys("123456");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“确认修改”按钮
        driver.findElement(By.cssSelector("body > div:nth-child(7) > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > div > div > div:nth-child(2) > button")).click();
        Thread.sleep(1000);
        String str = driver.findElement(By.cssSelector("#oldPassword_help > div")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert str.equals("请输入密码！");
    }

    /**
     * 测试八：修改账户密码失败-新密码为空并且密码输入不一致
     */
    public void updatePasswordFile2() throws IOException, InterruptedException {
        //刷新
        driver.navigate().refresh();
        //点击“安全设置”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.leftMenu___179-W > ul > li:nth-child(2) > span")).click();
        //点击“修改”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.ant-list.ant-list-split > div > div > ul > li > ul > li")).click();

        //输入旧密码
        driver.findElement(By.cssSelector("#oldPassword")).sendKeys("111111");
        //输入确认密码
        driver.findElement(By.cssSelector("#confirmPassword")).sendKeys("123456");
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“确认修改”按钮
        driver.findElement(By.cssSelector("body > div:nth-child(7) > div > div.ant-modal-wrap > div > div.ant-modal-content > div.ant-modal-footer > div > div > div:nth-child(2) > button")).click();
        Thread.sleep(1000);
        String str = driver.findElement(By.cssSelector("#password_help > div")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert str.equals("请输入密码！");
        Thread.sleep(1000);
        String str1 = driver.findElement(By.cssSelector("#confirmPassword_help > div")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert str1.equals("两次密码输入不一致");

    }
}

